package router

import (
	"gin-demo/config"
	"gin-demo/controllers"
	"gin-demo/dbUtil"
	_ "gin-demo/docs"
	"gin-demo/token"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

func Router() *gin.Engine {
	r := gin.Default() //默认路由引擎：包括 Logger and Recovery middleware
	//r:=gin.New() //没有任何中间件的路由引擎

	//r.Use(service.Middleware()) //普通中间件校验
	//r.Use(service.AuthMiddleware()) //登录中间件校验

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": config.Mysqldb,
		})
	})

	// 健康检查接口
	r.GET("/health", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"code":    http.StatusOK,
			"success": true,
		})
	})

	user := r.Group("/user")
	{
		user.GET("/list", controllers.GetUserList)
		user.GET("/info/:user_id", controllers.GetUserInfo)
	}

	//基本操作练习
	r.GET("/get", controllers.BasicGetData)
	r.POST("/post", controllers.BasicPostData)
	//重定向到另外的域名
	r.GET("/redirect1", func(c *gin.Context) {
		url := "http://www.baidu.com"                //重定向的URL
		c.Redirect(http.StatusMovedPermanently, url) //301永久重定向
	})
	//重定向到当前服务的其它路由
	r.GET("/redirect2", func(c *gin.Context) {
		c.Request.URL.Path = "/get" //重定向到 http://127.0.0.1:8080/get
		r.HandleContext(c)
	})
	r.GET("/curl", controllers.BasicCurlData)
	r.GET("/output", controllers.Output)

	//文件相关操作
	r.GET("/get-file", controllers.GetFile)
	r.POST("/upload-file", controllers.UploadFile)
	r.POST("/upload-file-batch", controllers.UploadFileBatch)

	//路由分组
	r1 := r.Group("/home") //分组的1级路径
	{                      //这里的大括号可有可无，加上大括号是为了方便清晰的阅读代码
		r11 := r1.Group("user") //分组的2级路径
		{
			r11.POST("/login", controllers.Login)            //请求路径： /home/user/login
			r11.POST("/third-login", controllers.ThirdLogin) //请求路径： /home/user/third-login
			r11.POST("/logout", controllers.Logout)          //请求路径： /home/user/logout
			r11.POST("/checkUserV1", controllers.CheckUserV1)
			r11.POST("/checkUserV2", controllers.CheckUserV2)
			r11.POST("/createUuid", controllers.CreateUuid)
			r11.POST("/register", controllers.Register) //---注册
			r11.GET("/userinfo", controllers.UserInfo)  //---获取用户信息
		}
	}

	//swagger 中间件
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	//cookie
	//r.Use(controllers.CookieAuth()) //cookie中间件
	r.GET("/cookie", controllers.Cookie)
	//session
	r.Use(controllers.SessionAuth()) //session中间件
	r.GET("/session", controllers.Session)

	//logrus记录日志
	r.GET("/logrusDemo1", controllers.LogrusDemo1)
	r.Use(controllers.LogMiddleware())
	r.GET("/LogrusDemo2", controllers.LogrusDemo2)

	//操作数据库
	r2 := r.Group("/db")
	{
		//mysql原生操作数据库
		r2.GET("sql/getUserList", dbUtil.MysqlGetUserList)
		r2.GET("sql/getUserInfo", dbUtil.MysqlGetUserInfo)
		r2.POST("sql/insert", dbUtil.MysqlInsertData)
		r2.PUT("sql/update", dbUtil.MysqlUpdateData)
		r2.DELETE("sql/delete", dbUtil.MysqlDeleteData)

		//xorm 操作数据库
		r2.GET("xorm/getUserList", dbUtil.XormGetUserList)
		r2.GET("xorm/getUserInfo", dbUtil.XormGetUserInfo)
		r2.POST("xorm/insert", dbUtil.XormInsertData)
		r2.PUT("xorm/update", dbUtil.XormUpdateData)
		r2.DELETE("xorm/delete", dbUtil.XormDeleteData)

		//gorm 操作数据库
		r2.GET("gorm/getUserList", dbUtil.GormGetUserList)
		r2.GET("gorm/getUserInfo", dbUtil.GormGetUserInfo)
		r2.POST("gorm/insert", dbUtil.GormInsertData)
		r2.PUT("gorm/update", dbUtil.GormUpdateData)
		r2.DELETE("gorm/delete", dbUtil.GormDeleteData)
	}

	//token签名身份认证
	r3 := r.Group("/token")
	{
		//HMAC方式生成token，以及通过中间件验证token
		r3.POST("hmac/createToken", token.HmacCreateToken)
		r3.POST("hmac/checkToken", token.HmacAuthMiddleware(), func(c *gin.Context) {
			c.String(http.StatusOK, "token验证通过")
		})
		//RSA方式生成token
		r3.POST("rsa/createToken", token.RsaCreateToken)
		r3.POST("rsa/checkToken", token.RsaAuthMiddleware(), func(c *gin.Context) {
			c.String(http.StatusOK, "RSA token验证通过")
		})
		//ECDSA方式生成token
		r3.POST("ecdsa/createToken", token.EcdsaCreateToken)
		r3.POST("ecdsa/checkToken", token.EcdsaAuthMiddleware, func(c *gin.Context) {
			c.String(http.StatusOK, "ECDSA token验证通过")
		})
	}
	return r
}
